package com.isczy.eduservice.controller.front;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isczy.eduservice.entity.EduCourse;
import com.isczy.eduservice.entity.frontvo.CourseFrontVo;
import com.isczy.eduservice.entity.frontvo.CourseWebVo;
import com.isczy.eduservice.entity.vo.ChapterVo;
import com.isczy.eduservice.service.EduChapterService;
import com.isczy.eduservice.service.EduCourseService;
import com.isczy.orderapi.OrderFeignService;
import com.isczy.servicebase.exceptionHandler.MyException;
import com.isczy.utils.DataResult;
import com.isczy.utils.JwtUtils;
import com.isczy.utils.ResultCode;
import com.isczy.utils.ordervo.CourseWebVoOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private OrderFeignService orderFeignService;

    /***
     * ***************************************
     * 功能描述:前台条件分页查询课程
     * @param page
     * @param limit
     * @param courseFrontVo
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:11】
     * @author modify:
     */
    @PostMapping("getFrontCourseList/{page}/{limit}")
    public DataResult getFrontCourseList(@PathVariable long page, @PathVariable long limit,
                                @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        try{
            Page<EduCourse> pageCourse = new Page<>(page,limit);
            Map<String,Object> map = courseService.getCourseFrontList(pageCourse,courseFrontVo);
            //返回分页所有数据
            return DataResult.builder().data(map).successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("前台条件分页查询课程失败")
                    .params(ArrayUtil.toString(new Object[]{page,limit,courseFrontVo}))
                    .build();
        }

    }

    /***
     * ***************************************
     * 功能描述:前台查询课程详情的方法
     * @param courseId
     * @return {@link DataResult}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-20 14:11】
     * @author modify:
     */
    @GetMapping("getFrontCourseInfo/{courseId}")
    public DataResult getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request) {
        try{
            //根据课程id，编写sql语句查询课程信息
            CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
            //根据课程id查询章节和小节
            List<ChapterVo> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);
            //根据课程id和用户id查询当前课程是否已经支付过了
            boolean buyCourse = false;
            String token = JwtUtils.getMemberIdByJwtToken(request);
            if (StrUtil.isNotEmpty(token)){
                buyCourse = orderFeignService.isBuyCourse(courseId, token);
            }

            return DataResult.builder()
                    .data("courseWebVo",courseWebVo)
                    .data("chapterVideoList",chapterVideoList)
                    .data("isBuy",buyCourse)
                    .successBuild();
        }catch (Exception e){
            throw MyException.builder()
                    .exception(e)
                    .code(ResultCode.ERROR)
                    .className(getClass().getName())
                    .methodName(Thread.currentThread().getStackTrace()[1].getMethodName())
                    .message("前台查询课程详情失败")
                    .params(courseId)
                    .build();
        }
    }

    /***
     * ***************************************
     * 功能描述:根据课程id查询课程信息
     * @param id
     * @return {@link CourseWebVoOrder}
     * @author create: TODO 人员:【ChangZiYang】类型:【新增方法】日期:【2021-01-21 15:47】
     * @author modify:
     */
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return courseWebVoOrder;
    }

}












